<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

class RelativesController extends AppController {

    var $name = "Relatives";

    function index($id) {
        $this->layout = "ajax";

        $this->Relative->recursive = -1;
        //$this->set('relatives', $this->Relative->findAllByCandidateId($id));
        $this->set('relatives', $this->Relative->find("all", array("conditions" => array("Relative.candidate_id" => $id))));
        $this->Relative->Candidate->recursive = -1;
        $ben = $this->Relative->Candidate->find('all', array('conditions' => array('Candidate.candidate_id' => $id), 'fields' => array('Candidate.id', 'Candidate.1er_nombre', 'Candidate.1er_apellido', 'Candidate.2do_apellido', 'Candidate.nro_ident', 'Candidate.tipo')));
        $this->set('conteo', count($ben));
        $this->set('cod', $this->Session->read('cod'));
        $this->set('candidates', $ben);
        $nom = $this->Relative->Candidate->field('1er_nombre', array('id' => $id));
        $apel1 = $this->Relative->Candidate->field('1er_apellido', array('id' => $id));
        $apel2 = $this->Relative->Candidate->field('2do_apellido', array('id' => $id));

        $this->set('nombre', "$nom $apel1 $apel2");
        $this->set('ben', $id);
    }

    function add($ben_id=null) {
        $this->set('est', $this->Session->read('estado'));
        $this->layout = "ajax";

        if (!empty($this->data)) {
            if ($this->Relative->save($this->data)) {
                $ide = $this->data['Relative']['candidate_id'];
                $this->Session->setFlash("Familiar adicionado exitosamente");
                $this->setAction('index', $ide);
            } else {
                $this->set('nombre', $this->Relative->Candidate->find('first', array('conditions' => array('Candidate.id' => $this->data['Relative']['candidate_id']))));
            }
        } else {
            $this->data['Relative']['candidate_id'] = $ben_id;
            $this->set('nombre', $this->Relative->Candidate->find('first', array('conditions' => array('Candidate.id' => $this->data['Relative']['candidate_id']))));
        }
    }

    function edit($id=null) {
        $this->set('est', $this->Session->read('estado'));
        $this->layout = "ajax";
        if (empty($this->data)) {
            $this->Relative->recursive = -1;
            //$this->data = $this->Relative->findById($id);
            $this->data = $this->Relative->find("first", array("conditions" => array("Relative.id" => $id)));
        } else {
            if ($this->Relative->save($this->data)) {
                $fam = $this->data['Relative']['candidate_id'];
                $this->Session->setFlash('Familiar Actualizado con exito');
                $this->setAction("index", $fam);
            } else {
                $this->Session->setFlash('error guardando datos');
            }
        }
    }

    function delete($id, $ben) {

        if ($this->Relative->delete($id)) {
            $this->Session->setFlash('Familiar Borrado con éxito');
            $this->setAction('index', $ben);
        }
    }

    function repetidos() {
        $id = $this->Session->read('proy_id');
        if ($id == "") {
            $this->Session->setFlash('No ha seleccionado Proyecto');
            $this->redirect(array('controller' => 'Proyects', 'action' => 'index'));
        } else {
            $this->Relative->recursive = -1;
            $ben = $this->Relative->query(
                            "SELECT pariente1.nro_ident, pariente1.1er_apellido, pariente1.nombres, proyecto.codigo, aspirante2.nro_ident, pariente2.nro_ident, pariente2.1er_apellido, pariente2.nombres
                            FROM relatives pariente1
                            LEFT JOIN relatives pariente2 ON pariente1.nro_ident = pariente2.nro_ident
                            LEFT JOIN candidates aspirante1 ON pariente1.candidate_id = aspirante1.id
                            LEFT JOIN candidates aspirante2 ON pariente2.candidate_id = aspirante2.id
                            LEFT JOIN proyects proyecto ON aspirante2.proyect_id = proyecto.id
                            WHERE pariente1.id != pariente2.id AND aspirante1.proyect_id = $id");

            $this->set('candidates', $ben);
            $this->set('cod', $this->Session->read('cod'));
        }
    }

}

?>
